public class MinAddToMakeValid {

    public int minAddToMakeValid(String s) {
        char[] chars = s.toCharArray();
        int ans = 0;
        int count = 0;
        for (int i = 0; i < s.length(); i++) {
            if (chars[i] == '(') {
                --count;
            } else {
                if (count == 0) {
                    ans++;
                } else {
                    count++;
                }
            }
        }
        return ans - count;
    }
}
